package com.tanhua.server.handler;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.tanhua.common.pojo.User;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.dubbo.server.api.UserPageInfoApi;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

/**
 * 动态封禁
 */
@Component
@Aspect
public class RunTimeMonitorAdvice {
    @Reference(version = "1.0.0")
    private UserPageInfoApi userPageInfoApi;

    //切入点，监控业务层接口
    @Pointcut("execution(* com.tanhua.server.service.QX*Service.*(..))")
    public void pt() {
    }

    @Around("pt()")
    public Object runtimeAround(ProceedingJoinPoint pjp) throws Throwable {
        //获取执行签名信息
        Signature signature = pjp.getSignature();
        //通过签名获取执行操作名称（方法名）
        String methodName = signature.getName();

        //所有查询的操作都可以放行
        if (ObjectUtil.contains(methodName, "find")) {
            Object o = pjp.proceed(pjp.getArgs());
            return o;
        }
        //判断该用户是否被动态禁用
        User user = UserThreadLocal.get();
        //判断该用户是否在冻结中
        Integer freezingRange = userPageInfoApi.queryUserFreezingRange(user.getId());
        if (ObjectUtil.isNotEmpty(freezingRange) && ObjectUtil.equal(freezingRange, 3)) {
            //表示用户是动态被被封禁中
            return null;
        }
        //表示没有被封禁,直接放行
        Object o = pjp.proceed(pjp.getArgs());
        return o;
    }
}
